package se.energimyndigheten.lampguiden;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.webkit.WebView;

import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.MenuItem;

import se.energimyndigheten.lampguiden.R;

public class HelpActivity extends SherlockFragmentActivity {
    public static final String EXTRA_HTML_PATH = "html_path";
    public static final String EXTRA_TITLE = "title";

    private static final String TAG = HelpActivity.class.getSimpleName();
    private String mPath;
    private WebView mWebView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSupportActionBar().setIcon(R.drawable.ic_fragetecken);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        try {
            final Intent intent = getIntent();
            mPath = intent.getStringExtra(EXTRA_HTML_PATH);
            String title = intent.getStringExtra(EXTRA_TITLE);
            if (title != null) {
            	setTitle(title);
            }
        } catch (NullPointerException npe) {
            Log.e(TAG, "NullPointerException - invalid intent or missing required extra.");
            finish();
            return;
        }
        setContentView(R.layout.webview_activity);
        mWebView = (WebView) findViewById(R.id.webview);
        loadHtmlFile();
    }

    /*** Handle action bar app icon click events. Go to start activity (dashboard). */
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        Log.v(TAG, "onOptionsItemSelected");

        final int menuId = item.getItemId();
        switch (menuId) {
        case android.R.id.home:
            finish();
            break;
        default:
            return super.onOptionsItemSelected(item);
        }
        return true;
    }
    
    private void loadHtmlFile() {
        String path = mPath != null ? mPath : "file::///android_asset/404.html";
        Log.v(TAG, "loadHtmlFile :: path: " + path);
        mWebView.loadUrl(path);
    }
}
